/*
 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
 */

#ifndef CO_MASTER_CONFIG__H_
#define CO_MASTER_CONFIG__H_
#include <stdint.h>

#include "canopen/canopen.h"
#include "canopen/co_io.h"
#include "canopen/io_board_api.h"

#define INVALID_GPIO_COUNT 0xFF
#define INVALID_NODE       0xFF

#define CO_MASTER_LOG_TAG  "CO_M"

#define CO_IO_BOARD_VERSION_SIZE 64

typedef enum {
    co_sdo_access_download = 1,
    co_sdo_access_upload   = 2,
    co_sdo_access_complete = 3
} co_sdo_access_type;

/* CANopen-IO Board Node List */
typedef enum {
    can_io_board_node1 = 1,
    can_io_board_node2 = 2,
    can_io_board_node3 = 3,
    can_io_board_node4 = 4
} can_io_board_node_t;

typedef struct {
    co_sdo_access_type access;
    uint16_t           index;
    uint8_t            sub_index;
    uint16_t           len;
    void              *data;
} sdo_access_t;

typedef struct {
    uint8_t          node;
    io_board_state_t state;
    uint8_t          input_cnt;
    uint8_t          output_cnt;
    uint8_t          output_def_value[CO_IO_OUTPUT_DATA_SIZE];
    uint8_t          output_value[CO_IO_OUTPUT_DATA_SIZE];
    uint8_t          output_actual_value[CO_IO_OUTPUT_DATA_SIZE];
    uint8_t          input_actual_value[CO_IO_OUTPUT_DATA_SIZE];
    char            *version;
} co_io_table_list_t;

#ifndef CO_MASTER_CONFIG_SKIP_OD_ACCESS
#define CO_MASTER_SKIP_OD_ACCESS 0
#else
#define CO_MASTER_SKIP_OD_ACCESS CO_MASTER_CONFIG_SKIP_OD_ACCESS
#endif

#ifdef __cplusplus
extern "C" {
#endif

co_io_table_list_t *get_io_node(const uint8_t node);

uint8_t get_io_node_size(void);

uint8_t foreach_can_io_node(uint8_t *start);

co_io_table_list_t *foreach_io_board_table(uint8_t *node_index);

sdo_access_t *foreach_od_table(uint8_t *od_table_index, const uint8_t node);


#ifdef __cplusplus
}
#endif

#endif /* CO_MASTER_CONFIG__H_ */
